public class CLLNode {
    int data;
    CLLNode next;

    public CLLNode(int data) {
        this.data = data;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }

    public CLLNode getNext() {
        return next;
    }

    public void setNext(CLLNode next) {
        this.next = next;
    }
}

class CircularLinkedList{

    /**
     * 循环链表长度
     * @param headNode:头结点
     * @return 返回链表长度
     */
    int CLLListLength(CLLNode headNode){
        int length = 0;
        CLLNode currentNode = headNode;
        while(currentNode != null){
            length++;
            currentNode = currentNode.getNext();
            if(currentNode == headNode){
                break;
            }
        }
        return length;
    }

    /**
     * 在链表首位插入节点
     * @param headNode
     * @param nodeToInsert
     */
    void CLLInsertBegin(CLLNode headNode, CLLNode nodeToInsert){
        CLLNode currentNode = headNode;
        while(currentNode.getNext() != headNode){
            currentNode.setNext(currentNode.getNext());
        }
        nodeToInsert.setNext(nodeToInsert);
        if(headNode == null){
            headNode = nodeToInsert;
        }else {
            nodeToInsert.setNext(headNode);
            currentNode.setNext(nodeToInsert);
            headNode = nodeToInsert;
        }
    }

    /**
     * 在链表末尾插入节点
     * @param headNode
     * @param nodeToInsert
     */
    void CLLInsertEnd(CLLNode headNode, CLLNode nodeToInsert){
        CLLNode currentNode = headNode;
        while(currentNode.getNext() != headNode){
            currentNode.setNext(currentNode.getNext());
        }
        nodeToInsert.setNext(nodeToInsert);
        if(headNode == null){
            headNode = nodeToInsert;
        }else{
            nodeToInsert.setNext(headNode);
            currentNode.setNext(nodeToInsert);
        }
    }

    /**
     * 删除链表头节点
     * @param headNode
     */
    void CLLDeleteBegin(CLLNode headNode){
        CLLNode currentNode = headNode;
        while(currentNode.getNext() != headNode){
            currentNode = currentNode.getNext();
            currentNode.setNext(currentNode);
        }
        currentNode.setNext(headNode.getNext());
        headNode = headNode.getNext();
        return;
    }

    /**
     * 打印循环链表
     * @param headNode
     */
    public void printCLLList(CLLNode headNode) {
        CLLNode currentNode = headNode;
        while(currentNode != null){
            System.out.println(currentNode.getData()+"->");
            currentNode = currentNode.getNext();
            if(currentNode == headNode){
                break;
            }
        }
        System.out.println("("+currentNode.getData()+")headnode");
    }
}

class CLLLinkedListTest{
    public static void main(String[] args) {
        CLLNode node1 = new CLLNode(1);
        CLLNode node2 = new CLLNode(2);
        CLLNode node3 = new CLLNode(3);
        CLLNode node4 = new CLLNode(4);

        CircularLinkedList circularLinkedList = new CircularLinkedList();
        circularLinkedList.CLLInsertBegin(node1,node4);
        circularLinkedList.CLLInsertEnd(node1,node2);
        circularLinkedList.CLLInsertEnd(node1,node3);
        circularLinkedList.CLLInsertEnd(node1,node4);

        circularLinkedList.printCLLList(node1);

    }
}
